/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-12-12
 * @Time  : 下午7:43
 * @brief :
 */

var createUi = require("common:widget/ui/base/base.js").createUi;

exports.Check = createUi({
    _data:{
        $code:false,
        $phone:false,
        $password:false,
        $rePassword:false,
        $qcode:false,
        $tip:false,
        onError:function(tip){}
    },


    check:function(){
        var $tip = this.data('$tip'),
            me = this;
        $tip && $tip.css('visibility','hidden');

        if(this.data('$phone')){
            var phone = $.trim(this.data('$phone').val());
            if(!phone || !/^\d{11}$/.test(phone)){
                $tip && $tip.css('visibility','visible').text('手机号码输入错误');
                me.trigger('error','手机号码输入错误');
                return false;
            }
        }

        if(this.data('$code')){
            var code = $.trim(this.data('$code').val());
            if(!/^\d{6}$/.test(code)){
                $tip && $tip.css('visibility','visible').text('手机验证码输入错误');
                me.trigger('error','手机验证码输入错误');
                return false;
            }
        }

        if(this.data('$qcode')){
            var qcode = $.trim(this.data('$qcode').val());
            if(!/^[0-9a-zA-Z]{4}$/.test(qcode)){
                $tip && $tip.css('visibility','visible').text('验证码输入错误');
                me.trigger('error','验证码输入错误');
                return false;
            }
        }


        if(this.data('$password')){
            var password = $.trim(this.data('$password').val());
            if(password.length<6 || password.length>16){
                $tip && $tip.css('visibility','visible').text('密码错误');
                me.trigger('error','密码错误');
                return false;
            }

            if(this.data('$rePassword')){
                var rePassword = $.trim(this.data('$rePassword').val());
                if(rePassword!=password){
                    $tip &&  $tip.css('visibility','visible').text('两次密码输入不一致');
                    me.trigger('error','两次密码输入不一致');
                    return false;
                }
            }
        }
        return true;
    }
});